<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            min-height: 100vh;
            background-color: #f5f5f5;
        }
        .sidebar {
            background-color: #ff5000;
            color: white;
            padding: 15px;
            display: flex;
            flex-direction: column;
            align-items: flex-start;
            width: 250px;
            min-height: 100vh;
        }
        .sidebar h1 {
            margin: 0 0 20px 0;
        }
        .menu {
            display: flex;
            flex-direction: column;
            gap: 20px;
            width: 100%;
        }
        .menu a {
            color: white;
            text-decoration: none;
            padding: 10px 15px;
            border-radius: 5px;
            transition: background-color 0.3s;
            width: 100%;
            box-sizing: border-box;
        }
        .menu a:hover {
            background-color: #e04a00;
        }
        .content {
            flex-grow: 1;
            padding: 20px;
            background-color: white;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            margin: 20px;
            border-radius: 10px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        table, th, td {
            border: 1px solid #ccc;
        }
        th, td {
            padding: 10px;
            text-align: left;
        }
        th {
            background-color: #f5f5f5;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input, .form-group select, .form-group textarea {
            width: calc(100% - 20px);
            padding: 10px;
            margin-bottom: 5px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        .form-group button {
            padding: 10px 20px;
            background-color: #333;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .form-group button:hover {
            background-color: #555;
        }
        .actions {
            padding: 20px;
            background-color: white;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            border-radius: 10px;
            margin: 20px;
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
        }
        .actions button {
            padding: 10px 20px;
            background-color: #333;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .actions button:hover {
            background-color: #555;
        }
    </style>
</head>
<body>
<div class="sidebar">
    <h1>用户管理</h1>
    <div class="menu">
        <a href="admin">管理员主页</a>
        <a href="adminproductcategories">商品类型管理</a>
        <a href="adminproduct">商品管理</a>
        <a href="adminorder">订单管理</a>
        <a href="login1">登出</a>
    </div>
</div>

<div class="content">
    <h2>用户管理</h2>
    <div class="actions">
        <button onclick="showUserList()">用户列表</button>
    </div>
    <div id="moduleContent"></div>
</div>

<script>
    function showUserList() {
        fetch('/users')
            .then(response => response.json())
            .then(data => {
                let users = data;
                let userRows = users.map(user => `
                    <tr>
                      <td>${user.account}</td>
                      <td>${user.name}</td>
                      <td>${user.phone}</td>
                      <td>${user.email}</td>
                      <td>${user.sex === 1 ? '男' : '女'}</td>
                      <td>${user.age}</td>
                      <td>
                        <button onclick="editUser(${user.id})">编辑</button>
                        <button onclick="deleteUser(${user.id})">删除</button>
                      </td>
                    </tr>
                `).join('');
                document.getElementById('moduleContent').innerHTML = `
                    <h3>用户列表</h3>
                    <table>
                      <thead>
                        <tr>
                          <th>用户名</th>
                          <th>姓名</th>
                           <th>电话</th>
                           <th>邮箱</th>
                          <th>性别</th>
                          <th>年龄</th>
                          <th>操作</th>
                        </tr>
                      </thead>
                      <tbody>
                        ${userRows}
                      </tbody>
                    </table>
                `;
            })
            .catch(error => console.error('Error fetching user data:', error));
    }

    function editUser(id) {
        window.location.href = `userupdate?userId=${id}`;
    }

    function deleteUser(id) {
        fetch(`/users/${id}`, {
            method: 'DELETE'
        })
            .then(response => {
                if (response.ok) {
                    alert('用户已删除');
                    showUserList();
                } else {
                    alert('删除用户时出错');
                }
            })
            .catch(error => console.error('删除用户时出错:', error));
    }

    document.addEventListener('DOMContentLoaded', showUserList);
</script>
</body>
</html>
